//
//  KTMarketHomeController.swift
//  KOLToken
//
//  Created by yangsan on 2020/2/26.
//  Copyright © 2020 iOS-team. All rights reserved.
//

import UIKit
import DynamicColor
import SnapKit
import SwiftyFitsize
import Alamofire
import Starscream
class KTMarketHomeController: UIViewController {

    private let kKTMarketHomeCell = "kKTMarketHomeCell"
    
    private var headerDataArray: [String] = []
    private var listDataArray: [String] = []
    
    // MARK: - super
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "行情"
        initValue()
        buildUI()
        requestMarketData()
        
//        KTWebSocket().connectSever(urlString: "")
        KTWebsocketManager.default.initData()
//        var request = URLRequest(url: URL(string: "ws://localhost:8080/")!)
//        request.timeoutInterval = 5 // Sets the timeout for the connection
//        request.setValue("someother protocols", forHTTPHeaderField: "Sec-WebSocket-Protocol")
//        request.setValue("14", forHTTPHeaderField: "Sec-WebSocket-Version")
//        request.setValue("chat,superchat", forHTTPHeaderField: "Sec-WebSocket-Protocol")
//        request.setValue("Everything is Awesome!", forHTTPHeaderField: "My-Awesome-Header")
//        var socket = WebSocket(request: request)
//        socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)
//        socket.delegate = self as! WebSocketDelegate
//        socket.connect()
//        socket.onEvent = {
//
//        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        marketListTableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    // MARK: - private
    private func buildUI() {
        view.backgroundColor = .white
        view.addSubview(marketListTableView)
    }
    
    
    
    // MARK: - lazy
    lazy var marketListTableView: UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.bounces = false
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = view.backgroundColor
        tableView.rowHeight = 56~
        tableView.separatorStyle = .none
        tableView.tableHeaderView = headerView
        tableView.tableHeaderView?.height = 185~
        return tableView
    }()
    
    lazy var headerView: KTAdview = {
        let headerView = KTAdview.init(frame: .zero)
        headerView.backgroundColor = .white
        return headerView
    }()
    
    // MARK: - netWork
    func requestMarketData() {
        
        #warning ("test - yangsan")
        headerDataArray = ["涨幅榜","跌幅榜","热搜榜"]
        marketListTableView.reloadData()
    }
}

extension KTMarketHomeController : UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        #warning ("test - yangsan")
        return 5
        return listDataArray.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
        return headerDataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: kKTMarketHomeCell) as? KTMarketHomeCell
        if cell == nil {
            cell = KTMarketHomeCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: kKTMarketHomeCell)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 35~
    }
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if headerDataArray.count == 0 {return nil}
        let view = KTMarketHomeSectionHeaderView.init(frame: .zero, title: headerDataArray[section])
        return view
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView.init()
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.navigationController?.pushViewController(KTCoinExchangeController(), animated: true)
    }
}

class KTMarketHomeSectionHeaderView: UIView {
    init(frame: CGRect, title: String) {
        super.init(frame: frame)
        buildUI(title: title)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - init
    private func buildUI(title: String) {
        backgroundColor = DynamicColor(hex: 0xEEF0F4)
        let titleLabel = UILabel.init()
        titleLabel.text = title
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.right.equalToSuperview()
            make.left.equalTo(24~)
            make.height.equalTo(30~)
        }
    }
}
